<template>
  <el-dialog
    title="预留规则"
    destroy-on-close
    append-to-body
    :visible.sync="show"
    width="650px"
    :before-close="cancel">
    <el-form ref="elForm" :model="form" size="small" label-width="100px">
      <div class="card">
        <el-row style="margin-bottom: -30px">
          <el-form-item label-width="0" prop="stockMode">
            <el-radio-group v-model="form.stockMode">
              <el-radio :label="1"> {{ $t('固定预留库存') }}</el-radio>
              <el-radio :label="2" style="margin-left: 140px">{{ $t('百分比预留库存') }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="12" style="padding-right: 30px">
            <p style="background-color: #eee; padding: 10px; border-radius: 5px">
              {{
                $t(
                  '每次采购上架时都按固定数量预留， 预留剩下的才变成云库存展示云库存剩下的数量会先回收 再加上采购上架数量进行计算',
                )
              }}
            </p>
          </el-col>
          <el-col :span="12">
            <p style="background-color: #eee; padding: 10px; border-radius: 5px">
              {{
                $t(
                  '每次采购上架时都按百分比数量预留， 预留剩下的才变成云库存展示云库存剩下的数量会先回收 再加上采购上架数量进行计算',
                )
              }}
            </p>
          </el-col>
        </el-row>
        <!-- 固定预留库存 -->
        <el-row v-if="form.stockMode == 1" style="margin: 30px 0">
          <el-form-item label-width="105px" :label="$t('固定预留数量') + ':'" prop="cloudPrice">
            <el-input-number
              style="margin-right: 10px"
              v-model="form.fixedNum"
              controls-position="right"
              :precision="0"
              :min="0" />
          </el-form-item>
        </el-row>
        <!-- 百分比预留库存 -->
        <el-row v-if="form.stockMode == 2" style="margin: 30px 0">
          <el-form-item label-width="60px" :label="$t('百分比') + ':'" prop="cloudPrice">
            <el-input-number
              style="margin-right: 10px"
              v-model="form.percent"
              controls-position="right"
              :precision="3"
              :min="0" />
            <span>%</span>
          </el-form-item>
        </el-row>
      </div>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">
        <!-- 关闭 -->
        {{ $t('关闭') }}
      </el-button>
      <el-button type="primary" @click="handleSubmit">{{ $t('确定') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { skuDistributorSetRule } from '@/api/distribution/sku/shopSkuPair'
export default {
  props: {
    /**
     * formData
     */
    formData: {
      type: Object,
      default: () => ({}),
    },

    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      form: {},
    }
  },
  computed: {},
  watch: {
    show: function (newValue) {
      if (newValue) {
        Object.keys(this.formData).forEach((key) => {
          this.$set(this.form, key, this.formData[key])
        })
        console.log(`this.form ==>`, JSON.parse(JSON.stringify(this.form)))
      }
    },
  },
  methods: {
    handleSubmit() {
      const data = JSON.parse(JSON.stringify(this.form))
      skuDistributorSetRule(data).then((res) => {
        if (res.code === 200) {
          this.$message({ type: 'success', message: res.msg })
          this.$emit('getList')
          this.cancel()
        }
      })
    },
    cancel() {
      this.$emit('update:show', false)
    },
  },
}
</script>
<style scoped lang="scss"></style>
